package com.card.game.log;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Vector;

import com.card.game.BasePlayer;
import com.card.game.GameResult;
import com.card.game.Player;
import com.card.game.event.EventType;
import com.card.game.room.SHRoom;
import com.google.common.collect.Lists;
import com.xiaoao.net.utils.StringUtil;
import com.xiaoao.net.utils.TimeUtils;

/**   
 * @Description: TODO(用一句话描述该文件做什么) 
 * @author siyunlong  
 * @date 2016年8月31日 下午1:53:02 
 * @version V1.0   
 */
public class ShResults {
	private List<ShResult> resultList = Lists.newArrayList();
	private String createDate;
	private String createTime;
	private int roomId;
	private transient List<ShLog> roomLogs = Lists.newArrayList();
	private transient SHRoom room;
	
	public ShResults(SHRoom room) {
		this.createDate = TimeUtils.formatSimpeTime(new Date());
		this.createTime = getTime();
		this.roomId = room.getId();
		this.room = room;
	}
	
	public void clear() {
		roomLogs.clear();
		room = null;
	}
	
	public void init() {
		if(!resultList.isEmpty()) {
			return;
		}
		Vector<BasePlayer> pls = room.getPlayerList();
		int baseScore = room.getBaseScore();
		for (int i = pls.size()-1; i >= 0; i--) {
			resultList.add(new ShResult(pls.get(i).getUser(),baseScore));
		}
	}
	
	public void calResult() {
		GameResult gameResult = room.getGameResult();
		if(gameResult.isLiuJu()) {
			return;
		}
		for (ShResult shResult : resultList) {
			Player player = room.getPlayerById(shResult.userId);
			if(gameResult.isZimo()) {
				if(gameResult.isWinUser(shResult.userId)) {
					shResult.zimo ++;
				}
			}else {
				if(gameResult.isWinUser(shResult.userId)) {
					shResult.hu ++;
				}
				if(gameResult.isDianPaoUser(shResult.userId)) {
					shResult.dianPao ++;
				}
			}
			shResult.mingGang += player.getMingGangList().size();
			shResult.anGang += player.getAnGangList().size();
			int score = gameResult.getFanScore(shResult.userId)
					+ gameResult.getGangScore(shResult.userId)
					+ gameResult.getOtherScore(shResult.userId);
			shResult.score += score;
		}
	}
	public int getPlayerScore(String userId) {
		for (int i = resultList.size()-1; i >= 0; i--) {
			if(StringUtil.equals(userId, resultList.get(i).userId)) {
				return resultList.get(i).getScore();
			}
		}
		return 0;
	}
	
//	private void doEvent(ShResult shResult,EventType eventType) {
//		if(eventType == EventType.AnGang
//				|| eventType == EventType.FangGang
//				|| eventType == EventType.GuoLuGang
//				) {
//			shResult.gang ++;
//		}else if(eventType == EventType.Hu
//				|| eventType == EventType.QiangGang
//				|| eventType == EventType.GangKai) {
//			shResult.hu++;
//		}else if(eventType == EventType.ZiMo
//				|| eventType == EventType.GangTouKaiHua
//				|| eventType == EventType.ZiMoDui
//				) {
//			shResult.zimo++;
//		}else if(eventType == EventType.DianPao) {
//			shResult.dianPao ++;
//		}
//	}

	public List<ShResult> getResultList() {
		return resultList;
	}
	public void addRoomLog(ShLog log) {
		this.roomLogs.add(log);
	}
	
	public List<ShLog> getRoomLogs() {
		return roomLogs;
	}

	public void saveScore() {
//		for (int i = resultList.size()-1; i >= 0; i--) {
//			ShResult res = resultList.get(i);
//			int score = res.score;
//			User user = UserUtils.getUserByPrimaryKey(res.userId);
//			if(user != null) {
//				if(score != 0) {
//					user.addScore(score);
//				}
//				user.userPlayBacks().addUserBack(this);
//				user.sendUserUpdateMessage();
//			}
//		}
	}
	public static String getTime() {
		SimpleDateFormat sdfDate = new SimpleDateFormat( "HH:mm:ss" );
		return sdfDate.format(new Date());
	}
	//=======================
	public ShResults(int roomId) {
		this.createDate = TimeUtils.GetCurrentTimeStamp();
		this.createTime = getTime();
		this.roomId = roomId;
	}
	public void addTest(String userId,String name,String img) {
		resultList.add(new ShResult(userId, name, img));
	}
	
}
